<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <div class="container">
        <div class="control">
            <button>选项1</button>
            <button>选项2</button>
            <button>选项3</button>
        </div>
        <div class="content">
            <div class="box" style="display:block">内容1</div>
            <div class="box">内容2</div>
            <div class="box">内容3</div>
        </div>
    </div>

    <script>
        // 构造函数 设置属性
        function Table(btn_selector, content_selector) {
            // 按钮选择
            this.btns = document.querySelectorAll(btn_selector)
            // 内容选择
            this.contents = document.querySelectorAll(content_selector)
            // 索引
            this.index = 0

            this.bindEvent()
            this.changTable()
            

        } 

        // 原型  设置属性  按钮事件绑定
        Table.prototype.bindEvent = function () {
            var self = this //this指向实例对象

            for (var i = 0; i < self.btns.length; i++) {
                // 记录dom对象的数据
                // console.log(self.btns.length);

                self.btns[i].index = i
                console.log(self.index);

                self.btns[i].onclick = function () {

                    self.index = this.index

                    self.changTable()
                }
            }
        }

        // 原型  设置属性  选项卡事件
        Table.prototype.changTable = function () {
            var self = this  //this指向实例对象
            for (var i = 0; i < self.contents.length; i++) {
                self.contents[i].style.display = 'none'
            }
            self.contents[self.index].style.display = 'block'
        }
        var tab = new Table('.control button', '.box')

    </script>
</body>

</html>